# -*- coding:utf-8 -*-
# @FileName : app\api\v1\history\services.py
# @Time     : 2025/11/28
# @Author   : 天空之城

"""历史记录API的业务逻辑 - DEPRECATED

此模块已被弃用，请使用 app.services.history_service.HistoryService 替代。
保留此模块是为了向后兼容，所有新代码请直接使用 app.services.history_service.HistoryService。

原功能已迁移至：
- HistoryService.list_histories_for_user()
- HistoryService.get_history_for_user()
- HistoryService.delete_history_for_user()
- HistoryService.delete_all_histories_for_user()
- HistoryService.export_histories_for_user()
"""
import warnings
from flask import g

from app.models import History
from app.extensions import db
from typing import List, Dict, Any, Optional
from app.services.history_service import HistoryService as NewHistoryService


def get_history_records(limit: int = 20, offset: int = 0) -> List[Dict[str, Any]]:
    """获取历史记录列表 - DEPRECATED
    
    Args:
        limit: 返回记录的数量上限，默认20
        offset: 分页偏移量，默认0
        
    Returns:
        List[Dict[str, Any]]: 历史记录列表，每个元素是一个历史记录的字典表示
        
    Raises:
        Exception: 数据库查询失败时抛出异常
    """
    warnings.warn(
        "get_history_records 函数已被弃用，请使用 HistoryService.list_histories_for_user 替代",
        DeprecationWarning,
        stacklevel=2
    )
    
    return NewHistoryService.list_histories_for_user(user_id=g.user.id, limit=limit, offset=offset)


def get_history_record(record_id: int) -> Optional[Dict[str, Any]]:
    """获取单条历史记录 - DEPRECATED
    
    Args:
        record_id: 历史记录的唯一ID
        
    Returns:
        Optional[Dict[str, Any]]: 历史记录的字典表示，如果不存在则返回None
        
    Raises:
        Exception: 数据库查询失败时抛出异常
    """
    warnings.warn(
        "get_history_record 函数已被弃用，请使用 HistoryService.get_history_for_user 替代",
        DeprecationWarning,
        stacklevel=2
    )
    
    return NewHistoryService.get_history_for_user(user_id=g.user.id, record_id=record_id)


def delete_history_record(record_id: int) -> bool:
    """删除单条历史记录 - DEPRECATED
    
    Args:
        record_id: 历史记录的唯一ID
        
    Returns:
        bool: 删除成功返回True，否则返回False
        
    Raises:
        Exception: 数据库操作失败时抛出异常
    """
    warnings.warn(
        "delete_history_record 函数已被弃用，请使用 HistoryService.delete_history_for_user 替代",
        DeprecationWarning,
        stacklevel=2
    )
    
    return NewHistoryService.delete_history_for_user(user_id=g.user.id, record_id=record_id)


def delete_all_history_records() -> int:
    """删除当前用户的所有历史记录 - DEPRECATED
    
    Returns:
        int: 删除的记录数量
        
    Raises:
        Exception: 数据库操作失败时抛出异常
    """
    warnings.warn(
        "delete_all_history_records 函数已被弃用，请使用 HistoryService.delete_all_histories_for_user 替代",
        DeprecationWarning,
        stacklevel=2
    )
    
    return NewHistoryService.delete_all_histories_for_user(user_id=g.user.id)


def export_history_records(format: str, include: List[str] = None, filters: Dict[str, Any] = None) -> Dict[str, Any]:
    """导出历史记录 - DEPRECATED
    
    Args:
        format: 导出格式，支持json、csv、pdf
        include: 包含的数据类型，可选
        filters: 筛选条件，可选
        
    Returns:
        Dict[str, Any]: 导出结果，包含导出的文件名、文件内容或下载链接等
        
    Raises:
        ValueError: 不支持的导出格式时抛出异常
        Exception: 导出过程中发生错误时抛出异常
    """
    warnings.warn(
        "export_history_records 函数已被弃用，请使用 HistoryService.export_histories_for_user 替代",
        DeprecationWarning,
        stacklevel=2
    )
    
    return NewHistoryService.export_histories_for_user(
        user_id=g.user.id,
        format=format,
        include=include,
        filters=filters
    )